package com.core.convenientCode.common;

import com.common.utils.DateUtils;
import com.common.utils.StringUtils;
import com.model.pojo.sys.SysGenTable;
import com.model.pojo.sys.SysGenTableColumn;
import org.apache.velocity.VelocityContext;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @Author: zql
 * @Description: 模板工具累
 * @Data: 15:11 2021/6/2
 */
public class VelocityUtils {

    /** 项目空间路径 */
    private static final String PROJECT_PATH = "main/java";

    /** mybatis空间路径 */
    private static final String MYBATIS_PATH = "main/resources/mapper";

    /** 默认上级菜单，系统工具 */
    private static final String DEFAULT_PARENT_MENU_ID = "3";

    public static List<String> getTemplateList()
    {
        List<String> templates = new ArrayList<String>();
        templates.add("vm/java/dto/CreateDTO.java.vm");
        templates.add("vm/java/dto/GetListDTO.java.vm");
        templates.add("vm/java/dto/UpdateDTO.java.vm");
        templates.add("vm/java/dto/UpdateListDTO.java.vm");
        templates.add("vm/java/dto/UpdateStatusDTO.java.vm");
        templates.add("vm/java/Controller.java.vm");
        templates.add("vm/java/dto/Excel.java.vm");
        templates.add("vm/java/Service.java.vm");
        templates.add("vm/java/dao/Mapper.java.vm");
        templates.add("vm/java/xml/Mapper.xml.vm");
        templates.add("vm/java/pojo/Pojo.java.vm");
        templates.add("vm/java/vo/GetVO.java.vm");
        templates.add("vm/java/vo/ErrorVO.java.vm");
        templates.add("vm/java/vo/ImportVO.java.vm");
        templates.add("vm/java/domain/GetDO.java.vm");
        return templates;
    }

    /**
     * @Author: zql
     * @Description: 获取生成的模板路径+文件名
     * @param template
     * @param genTable
     * @Data: 13:16 2021/11/19
     */
    public static String getFilePathName(String template, SysGenTable genTable)
    {

        // 文件名称
        String fileName = "";
        // 包路径
        String packageName = genTable.getPackageName();
        // 模块名
        String moduleName = genTable.getModuleName();
        // 大写类名
        String className = genTable.getClassName();
        // 小写类名
        String classNameUn = StringUtils.uncapitalize(genTable.getClassName());
        // 业务名称
        String businessName = genTable.getBusinessName();

        String javaPath = PROJECT_PATH + "/" + StringUtils.replace(packageName, ".", "/");
       // String mybatisPath = MYBATIS_PATH + "/" + moduleName;


        if (template.contains("CreateDTO.java.vm"))
        {
            fileName = StringUtils.format("{}/model/dto/{}/Create{}DTO.java", javaPath,classNameUn, className);
        }
        else if (template.contains("GetListDTO.java.vm"))
        {
            fileName = StringUtils.format("{}/model/dto/{}/Get{}ListDTO.java", javaPath,classNameUn, className);
        }
        else if (template.contains("UpdateDTO.java.vm"))
        {
            fileName = StringUtils.format("{}/model/dto/{}/Update{}DTO.java", javaPath,classNameUn, className);
        }
        else if (template.contains("UpdateListDTO.java.vm"))
        {
            fileName = StringUtils.format("{}/model/dto/{}/Update{}ListDTO.java", javaPath,classNameUn, className);
        }
        else if (template.contains("UpdateStatusDTO.java.vm"))
        {
            fileName = StringUtils.format("{}/model/dto/{}/Update{}StatusDTO.java", javaPath,classNameUn, className);
        }
        else if (template.contains("Controller.java.vm"))
        {
            fileName = StringUtils.format("{}/controller/v1/{}/{}Controller.java", javaPath, classNameUn,className);
        }
        else if (template.contains("Excel.java.vm"))
        {
            fileName = StringUtils.format("{}/model/dto/{}/{}Excel.java", javaPath,classNameUn, className);
        }
        else if (template.contains("Service.java.vm"))
        {
            fileName = StringUtils.format("{}/service/{}Service.java", javaPath, className);
        }
        else if (template.contains("Mapper.java.vm"))
        {
            fileName = StringUtils.format("{}/dao/{}Mapper.java", javaPath, className);
        }
        else if (template.contains("Mapper.xml.vm"))
        {
            fileName = StringUtils.format("{}/{}Mapper.xml", MYBATIS_PATH, className);
        }
        else if (template.contains("Pojo.java.vm"))
        {
                fileName = StringUtils.format("{}/model/pojo/{}/{}.java", javaPath,classNameUn, className);
        }
        else if (template.contains("GetVO.java.vm"))
        {
            fileName = StringUtils.format("{}/model/vo/{}/Get{}VO.java", javaPath,classNameUn, className);
        }
        else if (template.contains("ErrorVO.java.vm"))
        {
            fileName = StringUtils.format("{}/model/vo/{}/Error{}VO.java", javaPath,classNameUn, className);
        }
        else if (template.contains("ImportVO.java.vm"))
        {
            fileName = StringUtils.format("{}/model/vo/{}/Import{}VO.java", javaPath,classNameUn, className);
        }if (template.contains("GetDO.java.vm"))
        {
            fileName = StringUtils.format("{}/model/domain/{}/Get{}DO.java", javaPath,classNameUn, className);
        }
        return fileName;
    }

    /**
     * @Author: zql
     * @Description: 设置模板所需的变量方法
     * @param genTable
     * @param sysGenTableColumnList
     * @Data: 13:15 2021/11/19
     */
    public static VelocityContext prepareContext(SysGenTable genTable, List<SysGenTableColumn> sysGenTableColumnList)
    {
        String moduleName = genTable.getModuleName();
        String businessName = genTable.getBusinessName();
        String packageName = genTable.getPackageName();
        String tplCategory = genTable.getTplCategory();
        String functionName = genTable.getFunctionName();

        VelocityContext velocityContext = new VelocityContext();
        velocityContext.put("tplCategory", genTable.getTplCategory());
        velocityContext.put("tableName", genTable.getTableName());
        velocityContext.put("functionName", StringUtils.isNotEmpty(functionName) ? functionName : "【请填写功能名称】");
        velocityContext.put("ClassName", genTable.getClassName());
        velocityContext.put("className", StringUtils.uncapitalize(genTable.getClassName()));
        velocityContext.put("moduleName", genTable.getModuleName());
        velocityContext.put("BusinessName", StringUtils.capitalize(genTable.getBusinessName()));
        velocityContext.put("businessName", genTable.getBusinessName());
        velocityContext.put("basePackage", getPackagePrefix(packageName));
        velocityContext.put("packageName", packageName);
        velocityContext.put("author", genTable.getFunctionAuthor());
        velocityContext.put("datetime", DateUtils.getDate());
        //velocityContext.put("pkColumn", genTable.getPkColumn());
        velocityContext.put("importList", getImportList());
        velocityContext.put("permissionPrefix", getPermissionPrefix(moduleName, businessName));
        velocityContext.put("columns", sysGenTableColumnList);
        velocityContext.put("table", genTable);
        return velocityContext;
    }

    /**
     * 获取包前缀
     *
     * @param packageName 包名称
     * @return 包前缀名称
     */
    public static String getPackagePrefix(String packageName)
    {
        int lastIndex = packageName.lastIndexOf(".");
        String basePackage = StringUtils.substring(packageName, 0, lastIndex);
        return basePackage;
    }

    /**
     * 根据列类型获取导入包
     *
     * @return 返回需要导入的包列表
     */
    public static HashSet<String> getImportList()
    {

        HashSet<String> importList = new HashSet<String>();
        importList.add("java.util.List");
        importList.add("java.util.Date");
        return importList;
    }

    /**
     * 获取权限前缀
     *
     * @param moduleName 模块名称
     * @param businessName 业务名称
     * @return 返回权限前缀
     */
    public static String getPermissionPrefix(String moduleName, String businessName)
    {
        return StringUtils.format("{}:{}", moduleName, businessName);
    }
}
